package com.lan.app.api;


import com.lan.app.constant.Constant;
import com.lan.app.repository.Tpl;
import com.lan.app.repository.User;
import com.lan.app.service.ITplService;
import com.lan.support.intercept.an.Auth;
import com.lan.support.response.json.R;
import com.lan.support.page.Page;
import com.lan.support.page.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("api/tpl")
public class TplApi {

    @Resource
    private ITplService tplService;


    @ResponseBody
    @RequestMapping(value = "page")
    public R list(Tpl tpl, PageHelper helper) {
        Page<Tpl> page = tplService.getByPage(helper, tpl);
        return R.OK(page);
    }

    @Auth
    @ResponseBody
    @RequestMapping(value = "add")
    public R add(HttpSession session, Tpl tpl) {
        User user = (User) session.getAttribute(Constant.User_Info);
        tpl.setUid(user.getId());
        Integer id = tplService.save(tpl);
        return R.OK(id);
    }

    @Auth
    @ResponseBody
    @RequestMapping(value = "del/{id}")
    public R del(@PathVariable Integer id) {
        tplService.modifyDel(id);
        return R.OK();
    }

    @ResponseBody
    @RequestMapping(value = "update")
    public R update(Tpl tpl) {
        int result = tplService.updateById(tpl);
        return R.OK();
    }

    @ResponseBody
    @RequestMapping(value = "detail/{id}")
    public R detail(@PathVariable Integer id) {
        Tpl tpl = tplService.get(id);
        return R.OK(tpl);
    }
}
